<?php
namespace app\common\service;

use think\Page;
use app\common\logic\wechat\WechatUtil;
use think\facade\Db;

class Index extends Base {
    static $AUTH_CHECK = false; //默认必须登录

    public function index(){

        $where = [];
        $page_id = input('page_id');
        if($page_id){
            $where['id'] = $page_id;
        }else{
            $where['is_home'] = 1;
        }

        $pagedata = Db::name('designerpage')->where($where)->find();
        if(!$pagedata){
            return $this->json(['status'=>0,'msg'=>'页面不存在']);
        }
        $pagecontent = logic('PageLogic')->initpagecontent($pagedata['content']);

        $pageinfo = json_decode($pagedata['pageinfo'],true);
        $pageparams = $pageinfo[0]['params'];
        $rdata = [];
        $rdata['pageinfo'] = $pageparams;
        $rdata['pagecontent'] = $pagecontent;
        $rdata['copyright'] = tpCache('basic.copy_right');
        $rdata['copyright_link'] = tpCache('basic.copy_right_link');
        $result = [
            'status' => 1,
            'msg' => '',
            'data' => $rdata,
        ];
        return $result;
//        return json_encode($result);
    }
    public function reg(){
        $intro_id = input('pid');
        $parent = [];
        if($intro_id){
            $parent = Db::name('users')->where('user_id',$intro_id)->find();
        }
        $plate = platform();
        $store_name = tpCache('basic.store_name');
        $store_logo = tpCache('basic.store_logo');
        $rdata = [];
        $rdata['status'] = 1;
        $rdata['reg'] = tpCache('reg.reg_'.$plate); //注册登录
        $rdata['mobile_login'] = tpCache('reg.mobile_login_'.$plate); //手机验证码登录
        $rdata['auth_login'] = tpCache('reg.auth_login_'.$plate); //授权登录 h5时去掉授权登录
        if($plate=='h5'){
            $rdata['auth_login'] = 0;
        }
        $rdata['login_mast'] = tpCache('reg.login_mast_'.$plate); //授权登录 h5时去掉授权登录
        $rdata['name'] = $store_name;
        $rdata['logo'] = $store_logo;
        $rdata['reg_invite_code'] = 2;
        $rdata['reg_invite_code_text'] = '';
        $rdata['reg_invite_code_type'] =0;
        $rdata['xystatus'] = tpCache('xieyi.xieyi_status');
        $rdata['xyname'] = tpCache('xieyi.xieyi_name');
        $rdata['xycontent'] = tpCache('xieyi.xieyi_content');
        $rdata['xyname2'] = tpCache('xieyi.yinsi_name');
        $rdata['xycontent2'] = tpCache('xieyi.yinsi_content');
        $rdata['needsms'] = 0;
        $rdata['parent'] = $parent ? $parent : null;
        $rdata['platform'] = '';
        $rdata['login_bind_mobile'] = tpCache('reg.login_bind_mobile');

        //自定义登录设置
        $loginset =$this->loginset();
        $loginset_type = $loginset['loginset_type'];
        $loginset_data = $loginset['loginset_data'];
        $rdata['loginset_type'] = $loginset_type;
        $rdata['loginset_data'] = $loginset_data;
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $rdata,
        ];
        return $result;
    }

    public function loginset(){
        $loginset_type = 0;
        $loginset_data = '';
        $logo    = tpCache('basic.store_logo');;
        $sysname = tpCache('basic.store_name');;
        $bgcolor = tpCache('basic.color1');
        $titlecolor = tpCache('basic.color2');
        $loginset_data = array(
            "logo"      => $logo,
            "bgtype"    => 1,
            "bgcolor"   => $bgcolor,
            "bgimg"     => getCurUrl().'/static/admin/img/login/bg1.png',
            "cardcolor" => '#FFFFFF',
            "titletype" => 'center',
            "title"     => '欢迎使用'.$sysname,
            "titlecolor"=> $titlecolor,
            "subhead"   => '',
            "subheadcolor" => '#A8B5D3',
            "btntype"   => 1,
            "btncolor"   => '#0256FF',
            "btnwordcolor" => '#FFFFFF',
            "codecolor" => '#0256FF',
            "regtipcolor"  => '#666666',
            "regpwdbtncolor"  => '#666666',
            "xytipword"    => '我已阅读并同意',
            "xytipcolor"   => '#D8D8D8',
            "xycolor"  => '#0256FF'
        );
        return ['loginset_type'=>1,'loginset_data'=>$loginset_data];
        //读取登录设置信息
        $loginset = Db::name('design_login')->where('1=1')->field('id,data,type')->find();
        if($loginset){
            if(!empty($loginset['data'])){
                $loginset_data = json_decode($loginset['data'],true);
            }
            if(!empty($loginset['type'])){
                $loginset_type = $loginset['type'];
            }
        }else{

            $logo    = getCurUrl().'/static/imgsrc/logo.jpg';
            $sysname = '';
            $bgcolor = '';

            $loginset = array(
                'type'=>1,
                'updatetime'=>time(),
                'data'=>json_encode([
                    "logo"      => $logo,
                    "bgtype"    => 1,
                    "bgcolor"   => $bgcolor,
                    "bgimg"     => getCurUrl().'/static/admin/img/login/bg1.png',
                    "cardcolor" => '#FFFFFF',
                    "titletype" => 'center',
                    "title"     => '欢迎使用'.$sysname,
                    "titlecolor"=> '#000000',
                    "subhead"   => '',
                    "subheadcolor" => '#A8B5D3',
                    "btntype"   => 1,
                    "btncolor"   => '#0256FF',
                    "btnwordcolor" => '#FFFFFF',
                    "codecolor" => '#0256FF',
                    "regtipcolor"  => '#666666',
                    "regpwdbtncolor"  => '#666666',
                    "xytipword"    => '我已阅读并同意',
                    "xytipcolor"   => '#D8D8D8',
                    "xycolor"  => '#0256FF'
                ])
            );
            Db::name('design_login')->insertGetId($loginset);

            $loginset_type = 1;
            $loginset_data = $loginset_data = json_decode($loginset['data'],true);
        }

        return ['loginset_type'=>$loginset_type,'loginset_data'=>$loginset_data];
    }
    public function getregion(){
        $province_lists = Db::name('region')->where(['parent_id'=>0,'level'=>1])->field('id as value,name')->select()->toArray();
        foreach($province_lists as $k=>$province){
            $city_lists =  Db::name('region')->where(['parent_id'=>$province['value'],'level'=>2])->field('id as value,name')->select()->toArray();
            foreach($city_lists as $ck=>$city){
                $district_lists =  Db::name('region')->where(['parent_id'=>$city['value'],'level'=>3])->field('id as value,name')->select()->toArray();
                $city_lists[$ck]['district_lists'] = $district_lists;
            }
            $province_lists[$k]['city_lists'] = $city_lists;
        }
        $arr = ['value'=>'0','name'=>'请选择省','city_lists'=>['value'=>0,'name'=>'请选择市','district_lists'=>['value'=>'0','name'=>'请选择区县']]];
        array_unshift($province_lists,$arr);
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $province_lists
        ];
        return $result;
    }
    //检测app版本
    public function checkVersion(){
        $app_set = Db::name('app_set')->find();
        $describe = $app_set['describe'];
        $describe = str_replace("\r\n",'<br>',$describe);
        $describe = str_replace("\n",'<br>',$describe);
        $data = [
            // 版本更新内容 支持<br>自动换行
            'describe'=> $describe,
            'edition_url'=> $app_set['androidurl'], //apk、wgt包下载地址或者应用市场地址  安卓应用市场 market://details?id=xxxx 苹果store itms-apps://itunes.apple.com/cn/app/xxxxxx
            'edition_force'=> $app_set['edition_force'], //是否强制更新 0代表否 1代表是
            'package_type'=> 0, //0是整包升级（apk或者appstore或者安卓应用市场） 1是wgt升级
            'edition_issue'=> $app_set['edition_issue']?:0, //是否发行  0否 1是 为了控制上架应用市场审核时不能弹出热更新框
            'edition_number'=> $app_set['edition_number'], //版本号 最重要的manifest里的版本号 （检查更新主要以服务器返回的edition_number版本号是否大于当前app的版本号来实现是否更新）
            'edition_name'=> $app_set['edition_name'], // 版本名称 manifest里的版本名称
            'edition_silence'=> 0, // 是否静默更新 0代表否 1代表是
        ];
        return [
            'status' => 1,
            'data' => $data
        ];
    }

}
